import axios from 'axios';
import qs from 'qs';
import {toast} from "@/utils/notify"

export const request = (type, url, data, _callback,that, _errorBack) => {
    /*let accessToken = localStorage.getItem("accessToken");
    let tokenType = localStorage.getItem("tokenType");*/
    /*
    axios.defaults.headers.common['appId'] = localStorage.getItem('appId')||'';
    axios.defaults.headers.common['version'] = 10008;*/
    axios.defaults.headers.common['sessionId'] = localStorage.getItem('sessionId')||'';
    let config = {
        method: type,
        url: url,
        data: qs.stringify(data, {
            skipNulls: true
        }),
        requestHeader: {
            'Content-Type': 'application/json',
            "Access-Control-Allow-Origin": "*",
        },
    }
    axios(config).then(
        function (res) {
            if (res.data.code == 0) {
                _callback(res.data.data)
            } else if (res.data.code == 200002) {
                if (_errorBack) {
                    _errorBack()
                }
                localStorage.clear();
                toast('会话过期，请重新登录！');
                setTimeout(() => {
                    that.$router.push({
                        name:'author'
                    })
                }, 1000)
            } else {
                if (_errorBack) {
                    _errorBack()
                }
                toast(res.data.msg);
            }
        }
    ).catch(function (err) {
            if (err && err.response) {
                switch (err.response.status) {
                    case 400:
                        err.message = '请求错误'
                        break
                    case 401:
                        err.message = '未授权，请登录'
                        break
                    case 403:
                        err.message = '拒绝访问'
                        break
                    case 404:
                        err.message = `请求网络错误`;
                        break
                    case 408:
                        err.message = '请求超时'
                        break
                    case 500:
                        if (err.response.data.msg) {
                            err.message = err.response.data.msg
                        } else {
                            err.message = '服务器500错误'
                        }
                        break
                    case 501:
                        err.message = '服务未实现'
                        break
                    case 502:
                        err.message = '网关错误'
                        break
                    case 503:
                        err.message = '服务不可用'
                        break
                    case 504:
                        err.message = '网关超时'
                        break
                    case 505:
                        err.message = 'HTTP版本不受支持'
                        break
                    default:
                }
            }
            toast(err.message);
            if (_errorBack) {
                _errorBack()
            }
        }
    )
};